//
//  cCVCardViewController.m
//  cyChat
//
//  Created by 楼楚 on 15/12/8.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "cCVCardViewController.h"
#import "XMPPvCardTemp.h"
@interface cCVCardViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
/*
 头像
 */
@property (weak, nonatomic) IBOutlet UIImageView *headerView;
/*
 用户昵称
 */
@property (weak, nonatomic) IBOutlet UILabel *nicknameLabel;
/*
 聊天账号
 */
@property (weak, nonatomic) IBOutlet UILabel *chatnumLabel;

@end
@implementation cCVCardViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"个人信息";
    
    [self loadVCard];
}

/*
 加载电子名片
 */

- (void)loadVCard
{
    
    XMPPvCardTemp *myVCard = [cCXMPPTool sharedcCXMPPTool].vCard.myvCardTemp;
    
    //    设置头像
    if (myVCard.photo) {
        self.headerView.image = [UIImage imageWithData:myVCard.photo];
        
    }
    //    设置昵称
    self.nicknameLabel.text = myVCard.nickname;
    
    //    设置聊天账号
    NSString *user = [cCUserInfo sharedcCUserInfo].user;
    self.chatnumLabel.text = [NSString stringWithFormat:@"%@",user];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取cell的tag
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSInteger *tag = cell.tag;
//    判断
    if (tag == 0) {
//        选择照片
        UIActionSheet *sheet =[[UIActionSheet alloc]initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相册" otherButtonTitles:nil, nil];
        [sheet showInView:self.view];
        NSLog(@"这行放图片");
    }
    if (tag == 1){
//        跳到下一个控制器
        NSLog(@"这行是昵称");
    }else{
//        什么都不干
        NSLog(@"这行是账号");
    }
}
#pragma mark - ActionSheet的代理
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//   设置代理
    imagePicker.delegate = self;
//    设置允许编辑
    imagePicker.allowsEditing = YES;
    
    if (buttonIndex == 0) {
        //相册
        NSLog(@"相册");
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
    }
    
    
    else{
//取消
        return;
    }
//    显示图片选择器
    [self presentViewController:imagePicker animated:YES completion:nil];
}
#pragma mark - 图片选择器的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSLog(@"%@",info);
//    获取图片 设置图片
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    self.headerView.image = image;
    
//    隐藏窗口
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}

@end
